package com.jml.zlhq.app.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.graphics.Matrix;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jml.zlhq.app.R;
import com.jml.zlhq.app.util.UIUtil;

public class CommentListActivity extends BaseFragmentActivity {
	private ViewPager viewPager;//页卡内容
	private TextView textView1,textView2;
	private int currIndex;
	private ImageView cursor;
	private List<Fragment> views = new ArrayList<Fragment>();// Tab页面列表
	private int offset = 0;// 动画图片偏移量
	private int bmpW;// 动画图片宽度
    private DianPingFragment fragment1;  
    private PingLunFragment fragment2; 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.comment_list_activity);
		
		UIUtil.setWinWidthHight(this);
		
		LinearLayout leftLay = (LinearLayout) findViewById(R.id.content_header_left_lay);
		LinearLayout rightLay = (LinearLayout) findViewById(R.id.content_header_right_lay);
		leftLay.setOnClickListener(this);
		rightLay.setOnClickListener(this);
		AutoCompleteTextView centerEdit = (AutoCompleteTextView) findViewById(R.id.content_header_center_edit);
		centerEdit.setVisibility(View.GONE);
		ImageView leftImage = (ImageView) findViewById(R.id.content_header_left_img);
		leftImage.setImageResource(R.drawable.back_selector);
		ImageView roghtImage1 = (ImageView) findViewById(R.id.content_header_right_img);
		roghtImage1.setVisibility(View.GONE);
		TextView centerText = (TextView) findViewById(R.id.content_header_center_text);
		centerText.setText("评论");
		TextView rightText = (TextView) findViewById(R.id.content_header_right_text);
		rightText.setVisibility(View.INVISIBLE);
		TextView cityChosed = (TextView) findViewById(R.id.content_header_center_text2);
		cityChosed.setVisibility(View.GONE);
		
		viewPager = (ViewPager) findViewById(R.id.comment_list_viewpager);
		cursor = (ImageView) findViewById(R.id.main_cursor);
		textView1 = (TextView) findViewById(R.id.comment_list_dianping);
		textView2 = (TextView) findViewById(R.id.comment_list_pinglun);
				
		textView1.setOnClickListener(this);
		textView2.setOnClickListener(this);
		
		GradientDrawable gd = new GradientDrawable();//创建drawable
	    gd.setColor(getResources().getColor(R.color.color_e95847));
	    gd.setSize(UIUtil.winWidth/2, UIUtil.dip2px(appContext, 4));
	    cursor.setImageDrawable(gd);
		bmpW = UIUtil.winWidth/2;// 获取图片宽度
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;// 获取分辨率宽度
		offset = (screenW / 2 - bmpW) / 2;// 计算偏移量
		Matrix matrix = new Matrix();
		matrix.postTranslate(offset, 0);
		cursor.setImageMatrix(matrix);// 设置动画初始位置
		
		viewPager.setOffscreenPageLimit(1);
		views=new ArrayList<Fragment>();
		fragment1 = new DianPingFragment();
		fragment2 = new PingLunFragment();
		views.add(fragment1);
		views.add(fragment2);
		viewPager.setAdapter(new MyViewPagerAdapter(fragmentManager,views));
		viewPager.setCurrentItem(0);
		textView1.setTextColor(getResources().getColor(R.color.color_ff96b0));
		MyOnPageChangeListener changeListener = new MyOnPageChangeListener();
		viewPager.setOnPageChangeListener(changeListener);
		
		if(!appContext.isLogin()){
			UIUtil.openActivity(this, LoginActivity.class, null, 5);
		}else{
			fragment1.setFreshPage(true);
			fragment2.setFreshPage(true);
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode==5){
			if(appContext.isLogin()){
				fragment1.setFreshPage(true);
				fragment2.setFreshPage(true);
			}else{
				UIUtil.ToastMessage(appContext, "未登录");
				finish();
			}
		}
	}
	
	@Override
	public void loadData(Map<String, String> param) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void clearData() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.comment_list_dianping:
			if(currIndex!=0)viewPager.setCurrentItem(0);
			break;
		case R.id.comment_list_pinglun:
			if(currIndex!=1)viewPager.setCurrentItem(1);
			break;
		case R.id.content_header_left_lay:
			finish();
			break;
		default:
			break;
		}
		
	}
	
	public class MyViewPagerAdapter extends FragmentPagerAdapter{
		private List<Fragment> mListViews;
		
		public MyViewPagerAdapter(FragmentManager mFragmentManager,List<Fragment> mListViews) {
			super(mFragmentManager);
			this.mListViews = mListViews;
		}

		@Override
		public int getCount() {			
			return  mListViews.size();
		}
		
		@Override  
	    public Fragment getItem(int arg0) {  
	        return mListViews.get(arg0);  
	    }  
	    
	}

    public class MyOnPageChangeListener implements OnPageChangeListener{

    	int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量
//		int two = one * 2;// 页卡1 -> 页卡3 偏移量
//		int three = one * 3;// 页卡1 -> 页卡3 偏移量
		public void onPageScrollStateChanged(int arg0) {
			
			
		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {
			
			
		}

		public void onPageSelected(int position) {
			Animation animation = new TranslateAnimation(one*currIndex, one*position, 0, 0);
			currIndex = position;
			animation.setFillAfter(true);// True:图片停在动画结束位置
			animation.setDuration(300);
			cursor.startAnimation(animation);
			textView1.setTextColor(getResources().getColor(R.color.color_64));
			textView2.setTextColor(getResources().getColor(R.color.color_64));
			switch (position) {
			case 0:
				textView1.setTextColor(getResources().getColor(R.color.color_ff96b0));
				break;
			case 1:
				textView2.setTextColor(getResources().getColor(R.color.color_ff96b0));
				break;
			}
		}
    	
    }
	
}
